﻿using System;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace stringmanage
{
    public partial class Form_main : Form
    {
        public string src;

        public Form_main()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 程序字符
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            string s = this.richTextBox1.Text;
            s = s.Replace("\\", "\\\\");
            s = s.Replace("\"", "\\\"");
            this.richTextBox2.Text = s;
        }

        private void Form_main_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(src))
            {
                StreamReader sr = new StreamReader(src, Encoding.Default);
                this.richTextBox1.Text = sr.ReadToEnd();
                sr.Close();
            }

            this.comboBox1.SelectedIndex = 0;
        }

        /// <summary>
        /// 生成字符串
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            string s = "";

            string s1 = this.richTextBox1.Text;
            string s2 = s1.Replace("\\", "\\\\");
            s2 = s2.Replace("\"", "\\\"");
            char[] split = new char[] { '\n' };
            string[] temp = s2.Split(split);

            if (this.comboBox1.Text == "string_builder")
            {
                for (int i = 0; i < temp.Length; i++)
                {
                    if (temp[i].Trim().Length > 2)
                    {
                        if (!temp[i].Trim().Substring(0, 2).Equals("//"))
                        {
                            s += "strTrans.Append(\"" + temp[i] + "\\n\");\n";
                        }
                        else
                        {
                            s += temp[i] + "\n";
                        }
                    }
                    else
                    {
                        s += "strTrans.Append(\"" + temp[i] + "\\n\");\n";
                    }
                }
            }
            else if (this.comboBox1.Text == "+")
            {
                s += "strTrans=\"\";\n";
                for (int i = 0; i < temp.Length; i++)
                {
                    if (temp[i].Trim().Length > 2)
                    {
                        if (!temp[i].Trim().Substring(0, 2).Equals("//"))
                        {
                            s += "strTrans+=\"" + temp[i] + "\\n\";\n";
                        }
                        else
                        {
                            s += temp[i] + "\n";
                        }
                    }
                    else
                    {
                        s += "strTrans+=\"" + temp[i] + "\\n\";\n";
                    }
                }
            }

            this.richTextBox2.Text = s;
        }
    }
}